// =============================================================
// CIndexScript
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Jul. 10, 2006
//----------------------------------------------------------------------
#include "CIndexScript.h"
//----------------------------------------------------------------------
CIndexScript::CIndexScript() : CScript()
{
    // ------------------------------------------
    // set internal variables by default
    // ------------------------------------------

	// current working environments
	string planet_name = "search planet";
	default_params["@sp_planet_name"] = getPlanetName(planet_name);
	string planet_working_path = "/searchplanet/";
	default_params["@sp_planet_working_path"] = getPlanetWorkingPath(planet_working_path);
	// tdl-related variables
	default_params["@sp_tdl_file_name"] = "woorung.tdl";

	// nxp, sc, ss variables
	string lib_path;

	lib_path = "linguist";
	default_params["@naver_linguist_lib_path"] = getTheLastVersionLibraryPath(lib_path);

	lib_path = "sigmasearch";
	default_params["@naver_sigmasearch_lib_path"] = getTheLastVersionLibraryPath(lib_path);
	lib_path = "NeXearchPortal";
	default_params["@naver_nxp_lib_path"] = getTheLastVersionLibraryPath(lib_path);
	default_params["@sp_dict"] = "/naver/share/DICT";

    // general environment variables
    default_params["@sp_docs"] = "${sp_home}/docs";
    default_params["@sp_idx"] = "${sp_home}/idx";
    default_params["@sp_var"] = "${sp_home}/var";
    default_params["@sp_sbin"] = "${sp_home}/sbin";
    default_params["@sp_bin"] = "${sp_home}/bin";
    default_params["@sp_log"] = "${sp_home}/log";
    default_params["@sp_etc"] = "${sp_home}/etc";
    default_params["@sp_conf"] = "${sp_home}/conf";
    default_params["@sp_htdocs"] = "${sp_home}/htdocs";
    default_params["@sp_src"] = "${sp_home}/src";

    // time-related variables
	string datetime = "24092409";
    default_params["@sp_today"] = getDateTime(datetime);

    // rsync-related variables
    default_params["@sp_rsync_options"] = "-av";

	// user.spec general variables
	default_params["@uspec_max_num_of_section_to_index"] = "100";
	default_params["@uspec_language"] = "korean";
	default_params["@uspec_query.indexer"] = "KsgmtEword ${sp_dict} -";
	default_params["@uspec_section.indexer"] = "Kgrdy \"${sp_dict} qsize=10 maxtermlen=30 maxngtermlen=30 mintermlen=6\" -";
	default_params["@uspec_section.parameter"] = "0.75 2 0.1";

	// user.spec optional variables
	default_params["@uspec_sim_norm_mode"] = "0x3";
	default_params["@uspec_truncation_type"] = "0\t# 0: right, 1: both (default: 0)";
	default_params["@uspec_proximity_data"] = "0\t# 1: support proximity search (default: 0)";
	default_params["@uspec_dvec_data"] = "1\t# 1: write dvec data (default: 1)";
	default_params["@uspec_case_sensitive"] = "0\t# 1: save case_sensitive term (default: 0)";
	default_params["@uspec_save_topweightdocs"] = "1\t# 1: save top weighted docs in posting (default: 1)";
	default_params["@uspec_topweightdoccnt"] = "4096\t# num of docs kept in specially in posting (default: 4096)";
	default_params["@uspec_topweighttermcnt"] = "0\t# max number of unique terms in dvec entries (default: 0)";

	// index-related variables
	default_params["@index_num_of_processes"] = "2";
	default_params["@index_size_of_memory_per_process"] = "256";
	default_params["@index_size_of_memory_to_merge"] = "1000";
	default_params["@index_size_of_index_volume"] = "500";

	// nxp spec variables
	default_params["@nxpspec_server_type"] = "frall";
	default_params["@nxpspec_max_servers"] = "15";
	default_params["@nxpspec_start_servers"] = "4";
	default_params["@nxpspec_min_spare_servers"] = "2";
	default_params["@nxpspec_max_spare_servers"] = "7";
	default_params["@nxpspec_max_request_per_child"] = "300";
	default_params["@nxpspec_max_clients"] = "500";
	default_params["@nxpspec_back_log"] = "20";
	default_params["@nxpspec_trace_level"] = "0";


	setDevelopmentEnvironment(default_params);
}
//----------------------------------------------------------------------
CIndexScript::~CIndexScript()
{
}
//----------------------------------------------------------------------
string &CIndexScript::getPlanetName(string &planet_name)
{
	string command = "hostname -s";
	string result = executeShellCommand(command);

	planet_name = result;
	return planet_name;
}
//----------------------------------------------------------------------
string &CIndexScript::getPlanetWorkingPath(string &planet_working_path)
{
	char *pbuf = (char *)malloc(1024);
	pbuf = getcwd(pbuf, 1024);

	if(pbuf==NULL) {
		cout << "//////////////////////////////////////////////////" << endl;
		cout << "[get_planet_working_path] getcwd() error" << endl;
		cout << "//////////////////////////////////////////////////" << endl;
	}

	planet_working_path = pbuf;
	delete []pbuf;

	return planet_working_path;
}
//----------------------------------------------------------------------
void CIndexScript::setDevelopmentEnvironment(map<string,string> &parameters)
{
    string ld_lib_path = "LD_LIBRARY_PATH=";
    char *tmp = getenv("LD_LIBRARY_PATH");
    if(tmp!=NULL) {
        string current_ld_lib_path = tmp;
        int len = current_ld_lib_path.length();
        int found = -1;;
        
        found = current_ld_lib_path.find(parameters["@naver_lingusp_lib_path"]);
        if(found<0||found>=len) ld_lib_path += parameters["@naver_lingusp_lib_path"] + "lib:";  
    
        found = current_ld_lib_path.find(parameters["@naver_sigmasearch_lib_path"]);
        if(found<0||found>=len) ld_lib_path += parameters["@naver_sigmasearch_lib_path"] + "lib:";

        found = current_ld_lib_path.find(parameters["@naver_nxp_lib_path"]);
        if(found<0||found>=len) ld_lib_path += parameters["@naver_nxp_lib_path"] + "lib:";
    
        ld_lib_path += current_ld_lib_path;
    }
    
    else {
        ld_lib_path += parameters["@naver_lingusp_lib_path"] + "lib:";
        ld_lib_path += parameters["@naver_sigmasearch_lib_path"] + "lib:";
        ld_lib_path += parameters["@naver_nxp_lib_path"] + "lib:"; 
    }   
    
	strcpy(global_environment_variable, ld_lib_path.c_str());
	if(putenv(global_environment_variable)<0) {
        cout << "[set_development_environment] putenv() error : " << ld_lib_path << endl;       
        exit(-1);
    }   
    //else cout << "[set_development_environment] : " << getenv("LD_LIBRARY_PATH") << endl;   
}
//----------------------------------------------------------------------
string &CIndexScript::getDateTime(string &datetime)
{
	string command = "date +\%y\%m\%d\%H\%M\%S";
	string result = executeShellCommand(command);

	datetime = result;
	return datetime;
}
//----------------------------------------------------------------------
string &CIndexScript::getTheLastVersionLibraryPath(string &lib_name)
{
	char cmd[1024];
	sprintf(cmd, "ls -alsdF /naver/opt/%s*[0-9] | awk '{ n = $10; } END { print $10 }' | xargs", lib_name.c_str());

	string command = cmd;

	string result = executeShellCommand(command);

	lib_name = result;
	
	return lib_name;
}
//----------------------------------------------------------------------
